package com.hyjx.framework.component.task;

import com.hyjx.framework.common.util.SpringBeanUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
 * 任务监视器实现类
 */
public class JobMonitor implements Job {
    private static Logger logger = LoggerFactory.getLogger(JobMonitor.class);

    /**
     * 执行任务
     *
     * @param arg
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext arg) throws JobExecutionException {
        JobDataMap jdm = arg.getJobDetail().getJobDataMap();

        String taskid = jdm.getString("taskid");
        String taskname = jdm.getString("taskname");
        String classname = jdm.getString("classname");
        String methodname = jdm.getString("methodname");
        String param1 = jdm.getString("param1");
        String param2 = jdm.getString("param2");
        logger.debug("开始执行任务:" + taskname);

        try {
            String tasktype = jdm.getString("tasktype");
            if ("1".equals(tasktype)) {
                Object obj = SpringBeanUtil.getBean(classname);
                Method method;
                String result = "";
                try {
                    Class clazz = obj.getClass();
                    if ("doTask2".equals(methodname) || "doTask1".equals(methodname)) {
                        method = clazz.getMethod(methodname, String.class);
                    } else {
                        method = clazz.getMethod(methodname, null);
                    }

                    Type returnType = method.getGenericReturnType();
                    if ("class java.lang.String".equals(returnType.toString())) {
                        if ("doTask2".equals(methodname)) {
                            result = (String) method.invoke(Class.forName(classname).newInstance(), param1, param2);
                        } else if ("doTask1".equals(methodname)) {
                            result = (String) method.invoke(Class.forName(classname).newInstance(), param1);
                        } else {
                            result = (String) method.invoke(Class.forName(classname).newInstance(), null);
                        }
                    } else {
                        method.invoke(obj, null);
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
                TaskManager.saveTaskLog(taskid, taskname, result);
            } else if ("2".equals(tasktype)) {
                Method method;
                boolean implIJob = false;
                String result = "";
                try {
                    Class clazz = Class.forName(classname);
                    if ("doTask2".equals(methodname) || "doTask1".equals(methodname)) {
                        method = clazz.getMethod(methodname, String.class);
                    } else {
                        method = clazz.getMethod(methodname, null);
                    }
                    Type returnType = method.getGenericReturnType();
                    if ("class java.lang.String".equals(returnType.toString())) {
                        if ("doTask2".equals(methodname)) {
                            result = (String) method.invoke(Class.forName(classname).newInstance(), param1, param2);
                        } else if ("doTask1".equals(methodname)) {
                            result = (String) method.invoke(Class.forName(classname).newInstance(), param1);
                        } else {
                            result = (String) method.invoke(Class.forName(classname).newInstance(), null);
                        }
                    } else {
                        method.invoke(Class.forName(classname).newInstance(), null);
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
                TaskManager.saveTaskLog(taskid, taskname, result);
            } else {
                IBaseDao dao = (IBaseDao) SpringBeanUtil.getBean("baseDao");
                boolean b = dao.executeSql(classname);
                TaskManager.saveTaskLog(taskid, taskname, "执行SQL，返回结果为" + b);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TaskManager.saveTaskLog(taskid, taskname, "执行任务出错：" + e.getMessage().substring(0, 100));
        }
        logger.debug("完成执行任务:" + taskname);
    }
}